// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewModelBaseWp7.cs" company="XamlmNinja">
//   2010
// </copyright>
// <summary>
//   The view model base wp 7.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceStyleRatherThanMessengerStyle.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Windows;

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Messaging;

    using WP7Contrib.Common;
    using WP7Contrib.Logging;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.Services.Storage;

    /// <summary>
    /// The view model base wp 7.
    /// </summary>
    public abstract class ViewModelBaseWp7 : ViewModelBase
    {
        #region Constants and Fields

        /// <summary>
        /// The log.
        /// </summary>
        protected readonly ILog Log;

        /// <summary>
        /// The navigation service.
        /// </summary>
        private readonly INavigationService navigationService;

        /// <summary>
        /// The storage service.
        /// </summary>
        private readonly IStorageService storageService;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        protected ViewModelBaseWp7()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        protected ViewModelBaseWp7(IMessenger messenger)
            : base(messenger)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        protected ViewModelBaseWp7(INavigationService navigationService, ILog log)
            : this(navigationService, new NullStorageService(), null, log)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="storageService">
        /// The storage service.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        protected ViewModelBaseWp7(INavigationService navigationService, IStorageService storageService, ILog log)
            : this(navigationService, storageService, null, log)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        protected ViewModelBaseWp7(INavigationService navigationService, IMessenger messenger, ILog log)
            : this(navigationService, new NullStorageService(), messenger, log)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBaseWp7"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="storageService">
        /// The storage service.
        /// </param>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        protected ViewModelBaseWp7(
            INavigationService navigationService, IStorageService storageService, IMessenger messenger, ILog log)
            : base(messenger)
        {
            this.navigationService = navigationService;
            this.storageService = storageService;
            this.Log = log;

            var assemblies = new List<Assembly> { this.GetType().Assembly, typeof(BaseModel).Assembly };
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="ViewModelBaseWp7"/> class. 
        /// </summary>
        ~ViewModelBaseWp7()
        {
            this.Dispose();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets NavigationService.
        /// </summary>
        public INavigationService NavigationService
        {
            get
            {
                return this.navigationService;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        public void Activate()
        {
            using (var storage = this.storageService.OpenTransient(this.GetType().Name))
            {
                this.IsBeingActivated(storage);
            }
        }

        /// <summary>
        /// The deactivate.
        /// </summary>
        public void Deactivate()
        {
            using (var storage = this.storageService.OpenTransient(this.GetType().Name))
            {
                this.IsBeingDeactivated(storage);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The execute on ui thread.
        /// </summary>
        /// <param name="uiAction">
        /// The ui action.
        /// </param>
        protected void ExecuteOnUIThread(Action uiAction)
        {
            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                uiAction();
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(uiAction);
            }
        }

        /// <summary>
        /// The is being activated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected abstract void IsBeingActivated(IStorage storage);

        /// <summary>
        /// The is being deactivated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected abstract void IsBeingDeactivated(IStorage storage);

        /// <summary>
        /// The set property and notify.
        /// </summary>
        /// <param name="currentValue">
        /// The current value.
        /// </param>
        /// <param name="newValue">
        /// The new value.
        /// </param>
        /// <param name="proertyName">
        /// The proerty name.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        protected void SetPropertyAndNotify<T>(ref T currentValue, T newValue, string proertyName)
        {
            if (Equals(currentValue, newValue))
            {
                return;
            }

            currentValue = newValue;
            this.RaisePropertyChanged(proertyName);
        }

        #endregion
    }
}